
const readline = require('readline')
const rl = readline.createInterface({
    input:process.stdin
})
let buf=[];
let i =0;
rl.on("line",line=>{
    if (line.trim() == "") rl.close()


buf.push(line);
})
rl.on("close",()=>{
    var total = buf[0],n=[],sum=0,temp=0,sumar=[],index=2,newar=[],index2=0,str="",bug=[];
n.push(Number(buf[1]))
for (let i =0;i<total-1;i++){
    sum += n[i]
    temp = n[i];
    n.push(Number(buf[sum+2+i]))
}
sum+=n[total-1]
for (let i=0;i<total;i++) {
    for (let j = 0; j < n[i]; j++) {
        sumar.push(buf[index + j]+" "+(Number(i)+1))
        index2 = sumar.length-1
        for (let x= sumar.length-1;x>0;x--){
            if(Number(buf[index+j].split(" ")[1])>Number(sumar[x-1].split(" ")[1])){
                temp = sumar[index2]
                sumar[index2] = sumar[x-1]
                sumar[x-1] =temp
                index2 = x-1;
            }
        }
    }
    index += n[i] + 1
}

var bug=1,bug2=0;
for (let i=0;i<sumar.length;i++) {
    newar.push(sumar[i].split(" ")[0] + " " + (bug+ " ") + sumar[i].split(" ")[2])
    if (i != sumar.length - 1) {
        if (Number(sumar[i + 1].split(" ")[1]) < Number(sumar[i].split(" ")[1])) {
            bug =i+2
        }
    }
}

var bug4=0,ls=0;
for (let i=0;i<total;i++){
    bug4=1,index=-1,ls=0;
    for (let j=0;j<sumar.length;j++){
        if (Number(sumar[j].split(" ")[2])==i+1){
            ls++;
            if (index!=-1&&Number(sumar[j].split(" ")[1])<Number(sumar[index].split(" ")[1])){
                bug4 = ls;
            }
            newar[j] += " "+bug4
            index=j;

        }
    }
}
str +=sum;
newar.forEach(function (v) {
    str += "\n"+v
})
console.log(str)
})

